package com.cafe.action.admin;

import java.util.HashMap;
import java.util.Map;

import org.hibernate.criterion.DetachedCriteria;
import org.springframework.beans.factory.annotation.Autowired;

import com.cafe.exception.CafeException;
import com.cafe.model.Order;
import com.cafe.model.OrderState;
import com.cafe.service.OrderService;
import com.malamute.core.action.BaseAction;
import com.malamute.core.action.CrudMethod;
import com.malamute.core.common.SearchParamUtil;
import com.malamute.core.exception.BusinessException;

/**
 * @author Aceguan
 * @description admin 订单Action
 * @date 2012-2-27 下午09:21:14
 * @version v1.0
 */
public class OrderAction extends BaseAction implements CrudMethod {
	@Autowired
	private OrderService orderService;

	private Order order;

	private Map<String, String> orderStateMap = new HashMap<String, String>();

	public Order getOrder() {
		return order;
	}

	public void setOrder(Order order) {
		this.order = order;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @return
	 * 
	 * @throws Exception
	 * 
	 * @see com.malamute.core.action.CrudMethod#save()
	 */
	@Override
	public String save() throws Exception {
		throw new CafeException("Order Save 方法不可访问");
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @return
	 * 
	 * @throws Exception
	 * 
	 * @see com.malamute.core.action.CrudMethod#update()
	 */
	@Override
	public String update() throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @return
	 * 
	 * @throws Exception
	 * 
	 * @see com.malamute.core.action.CrudMethod#input()
	 */
	@Override
	public String form() throws Exception {
		return FORM;
	}

	public String sendOrder() {
		if (order != null) {
			order = orderService.getOrderById(order.getId());
			order.setState(OrderState.SEND);
			orderService.updateOrder(order);
		} else {
			throw new BusinessException("order is not found");
		}
		return ACTION;

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @return
	 * 
	 * @throws Exception
	 * 
	 * @see com.malamute.core.action.CrudMethod#list()
	 */
	@Override
	public String list() throws Exception {
		DetachedCriteria criteria = DetachedCriteria.forClass(Order.class);
		SearchParamUtil.simpleSeach(criteria, searchParam);
		orderService.listOrdersByPage(criteria, page);
		return LIST;
	}

	public String viewDetailById() {
		order = orderService.getOrderById(order.getId());
		order.getOrderProducts();
		return "detail";
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @return
	 * 
	 * @throws Exception
	 * 
	 * @see com.malamute.core.action.CrudMethod#search()
	 */
	@Override
	public String search() throws Exception {
		return null;
	}

	public Map<String, String> getOrderStateMap() {
		for (OrderState o : OrderState.values()) {
			orderStateMap.put(o.getKey(), o.getValue());
		}
		return orderStateMap;
	}

}
